#include "projeca.hpp"
#include "travelling_type.hpp"
#include "travelling.hpp"
#include "refuelling.hpp"
#include "expense.hpp"


// default date = today
boost::gregorian::date const projeca::init_date(boost::gregorian::date _date)
{
	boost::gregorian::date d(boost::gregorian::not_a_date_time);
	
	if(_date == d)
		_date = boost::gregorian::day_clock::local_day();

	return _date;
}


float projeca::travelling(boost::gregorian::date _from_date, boost::gregorian::date _to_date, int _car_id)
{

	_to_date = init_date(_to_date);
	
	float km(travelling_csv::total_km(_car_id,_from_date, _to_date));

//	std::cout << "[projeca::travelling] TOT KM FROM DATE "<< _from_date << " TO DATE "<< _to_date << " = " << km << std::endl;

	return km;

}


float projeca::fuel_physical(boost::gregorian::date _from_date, boost::gregorian::date _to_date, int _car_id)
{
	_to_date = init_date(_to_date);
	
	float lt(refuelling_csv::total_fuel(_car_id, _from_date, _to_date));

	std::cout << "[projeca::fuel_physical] TOT LITERS FROM DATE " << _from_date << " TO DATE " << _to_date << " = " << lt << std::endl;
	
	return lt;
	
}


float projeca::fuel_financial(boost::gregorian::date _from_date, boost::gregorian::date _to_date, int _car_id)
{
	_to_date = init_date(_to_date);

	float amount(refuelling_csv::total_amount(_car_id, _from_date, _to_date));
	
	std::cout << "[projeca::fuel_financial] TOT MONEY SPENT FROM DATE " << _from_date << " TO DATE " << _to_date << " = " << amount << std::endl;

	return amount;
	
}

float projeca::travelling_fuel_refund(boost::gregorian::date _from_date, boost::gregorian::date _to_date, int _car_id)
{
	float amount(0.);
	
	_to_date = init_date(_to_date);
	
	amount = travelling_csv::fuel_refund(_car_id, _from_date, _to_date);
	
	std::cout << "[projeca::travelling_fuel_refund] total fuel refund from date " << _from_date << " to date " << _to_date << " = " << amount << std::endl;
	
	return amount;
}


float projeca::mean_fuel_consumption(boost::gregorian::date _first_date, boost::gregorian::date _last_date, int _car_id, bool _include_last_date)
{
	float consumption(0.);

	_last_date = init_date(_last_date);

	consumption = refuelling_csv::mean_fuel_consumption(_car_id, _first_date, _last_date, _include_last_date);

	std::cout << "[projeca::mean_fuel_consumption] MEAN FUEL CONSUMPTION (lt/km) = " << consumption << std::endl;

	return consumption;
}

/*
float projeca::estimated_fuel_consumption(boost::gregorian::date _date, int _car_id, bool _include_last_date)
{
	float consumption_in_date(0);

	consumption_in_date = refuelling_csv::estimated_fuel_consumption(_car_id, _date, _include_last_date);

	std::cout << "ESTIMATED FUEL CONSUMPTION IN DATE " << _date << " = " << consumption_in_date << std::endl;

	return consumption_in_date;
	
}*/


float projeca::mean_fuel_distance(boost::gregorian::date _first_date, boost::gregorian::date _last_date, int _car_id, bool _include_last_date)
{
	_last_date = init_date(_last_date);

	float distance(refuelling_csv::mean_fuel_distance(_car_id, _first_date, _last_date, _include_last_date));

	std::cout << "[projeca::mean_fuel_distance] MEAN DISTANCE PER LITER (km/lt) = " << distance << std::endl;

	return distance;
}


float projeca::travelling_estimated_fuel_consumption_physical(boost::gregorian::date _first_date, boost::gregorian::date _last_date, int _car_id, bool _include_last_date)
{
	float lt(0.);

	_last_date = init_date(_last_date);

	lt = travelling_csv::estimated_fuel_consumption_physical(_car_id, _first_date, _last_date, _include_last_date);

	//std::cout << "[projeca::travelling_estimated_fuel_consumption_physical] TRAVELLING ESTIMATED FUEL CONSUMPTION (lt) = " << lt << std::endl;

	return lt;
}

float projeca::travelling_estimated_fuel_consumption_financial(boost::gregorian::date _first_date, boost::gregorian::date _last_date, int _car_id, bool _include_last_date)
{
	float amount(0.);
	
	_last_date = init_date(_last_date);

	amount = travelling_csv::estimated_fuel_consumption_financial(_car_id, _first_date, _last_date, _include_last_date);

	std::cout << "[projeca::travelling_estimated_fuel_consumption_currency] estimated fuel consumption for business travelling from date " << _first_date << " to date " << _last_date << " (euro) = " << amount << std::endl;

	return amount;
}


float projeca::travelling_estimated_net_fuel_refund(boost::gregorian::date _from_date, boost::gregorian::date _to_date, int _car_id)
{
	float fuel_cost(travelling_estimated_fuel_consumption_financial(_from_date, _to_date, _car_id));
	float travelling_refund(travelling_fuel_refund(_from_date, _to_date, _car_id));
	
	travelling_refund -= fuel_cost;
	
	std::cout << "[projeca::travelling_estimated_net_fuel_refund] travelling estimated net fuel refund (euro) = " << travelling_refund << std::endl;
	
	return travelling_refund;
}


float projeca::kilometer_report(boost::gregorian::date _from_date, boost::gregorian::date _to_date, bool _include_last_date, int _plate_id)
{
	std::ofstream fp("kilometer_report", ios::out);
        float km(travelling_csv::kilometer_report(_plate_id, _from_date, _to_date, _include_last_date, &fp));

	fp.close();

	return km;
}


std::pair<float, float> const projeca::report_expenses(boost::gregorian::date _from_date, boost::gregorian::date _to_date, bool _include_last_date, int _empl_id)
{
	std::ofstream fp("expense_report", ios::out);
	std::pair<float, float> expenses(expense_csv::report_expenses(_empl_id, _from_date, _to_date, _include_last_date, &fp));

	fp << std::endl;
	fp.close();

	return expenses;
}

float projeca::kilometer_report_in_period(boost::gregorian::date _from_date, boost::gregorian::date _to_date, bool _include_last_date, int _plate_id)
{
	std::ofstream fp("kilometer_report", ios::out);
	float km(projeca::fuel_financial(_from_date, _to_date, _plate_id));

	fp << std::endl << "TOTAL AMOUNT = " << km << " euro, from date " << _from_date;
	fp << " to date " << _to_date << std::endl;
	fp << "TOTAL KILOMETERS = " << projeca::travelling(_from_date, _to_date, _plate_id) << " km, from date " << _from_date;
	fp << " to date " << _to_date << std::endl;
	fp << std::endl;

	fp.close();

	return km;
}
